Skip to content

114 testing testing [coverage edition]#116

Merged
cubap merged 26 commits intomainfrom
114-testing-testing
Apr 16, 2026
Merged

114 testing testing [coverage edition]#116
cubap merged 26 commits intomainfrom
114-testing-testing

Conversation

@cubap
Copy link
Copy Markdown
Member

@cubap cubap commented Apr 16, 2026

This pull request significantly expands and improves the automated test coverage for the application's REST API routes and middleware, focusing on robust error handling, edge cases, and correct propagation of upstream errors. The new and updated tests ensure that the application behaves correctly under a wide variety of failure scenarios, including network errors, malformed requests, and upstream service issues.

Key changes include:

Error Handling and Propagation:

  • Added comprehensive tests for error messenger middleware, covering scenarios such as already-sent headers, use of status code/message fallbacks, proper handling of upstream plain text and JSON error responses, and correct early returns when headers are sent. Also added unit tests for various messenger branches using a mock response object. [1] [2] [3] [4]
  • Enhanced tests for create, delete, overwrite, and query routes to verify that upstream error messages are preserved when possible, and that generic error messages are used as fallbacks when upstream .text() methods throw. Also added checks for correct status codes when upstream payloads are missing required fields. [1] [2] [3] [4] [5]

Middleware and Route Edge Cases:

  • Added tests for the verifyJsonContentType middleware to ensure it returns appropriate errors when the Content-Type header is missing or contains multiple values.
  • Introduced CORS middleware tests to verify that CORS headers are correctly added when enabled via environment variable.
  • Added tests to the index route to confirm that unsupported HTTP methods on the root path return a 405 status.

Upstream Communication and Timeout Handling:

  • Added tests for the fetchRerum utility to ensure correct handling of timeouts (mapping to 504), non-timeout network errors (mapping to 502), forwarding of abort signals, and fallback to default timeout when configuration is invalid.

These improvements greatly increase confidence in the application's resilience and correctness in the face of both expected and unexpected error conditions.


Error Handling and Propagation:

  • Expanded error messenger middleware tests for scenarios like already-sent headers, status code/message fallbacks, upstream plain text/JSON errors, and added unit tests for messenger branches with a mock response object. [1] [2] [3] [4]
  • Improved tests for create, delete, overwrite, and query routes to ensure correct error propagation and fallback messaging when upstream services fail or return malformed responses. [1] [2] [3] [4] [5]

Middleware and Route Edge Cases:

  • Added tests for verifyJsonContentType middleware to handle missing or multiple Content-Type headers.
  • Added CORS middleware tests to confirm headers are set when enabled.
  • Added tests to the index route for unsupported HTTP methods returning 405.

Upstream Communication and Timeout Handling:

  • Added fetchRerum tests for timeout mapping (504), network error mapping (502), abort signal forwarding, and default timeout fallback.

cubap and others added 26 commits April 13, 2026 14:12
Add a testing modernization plan and migrate test layout and CI to the new approach. Remove the legacy Jest config and delete legacy __tests__ artifacts, add a new top-level test/ suite with helpers (including test/helpers/env.js) and route test files, and update docs and CI to run the consolidated npm script (allTests) and expose targeted scripts (coreTests, existsTests, functionalTests). Dev dependencies and package metadata were adjusted to support the new runner/coverage tools.
Introduce a GitHub Actions test matrix (fast and full gates) to run CI test groups. Add a Playwright-based browser smoke test (test/e2e/ui-smoke.test.js) that launches the app and verifies basic UI flows. Update README with local instructions for running e2e (install browsers) and the ci:fast/ci:full command groups.
Introduce launchBrowserOrSkip helper to centralize Chromium launch and skip logic when not installed, and refactor existing smoke test to use it. Add several end-to-end UI smoke tests: successful create submission (stubbing /create), client-side JSON validation that prevents network calls, query form results (stubbing /query), and overwrite conflict handling (stubbing /overwrite returning 409). Tests use Playwright routing to mock server responses and assert flash messages and object viewer output.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Capture fetch URL/options in test mocks and add assertions to verify the upstream RERUM contract (URL, HTTP method, Authorization header, Content-Type). Updates test/routes/{create,delete,overwrite,query,update}.test.js to record lastFetchUrl/lastFetchOptions and assert correct endpoint/method/headers, and documents the new validation in test/TESTING.md with an example and rationale to catch breaking changes.
Update test suites to accept application/ld+json by configuring express.json({ type: ['application/json', 'application/ld+json'] }) across create, query, delete, overwrite, and update tests. Add new tests: create and query now verify requests with Content-Type application/ld+json are accepted. Add tests that sending text/plain yields 415 in create, delete, overwrite, and update. Import and mount the error messenger in delete, overwrite, and update tests. Add network-failure cases that map rejected fetch to a 502 response for overwrite and update.
Add tests to exercise create route behavior: ensure request body id is converted to _id before upstream, and verify a successful upstream response missing id fields is treated as an error (502). Add new tests for verifyJsonContentType middleware to ensure it returns 415 when the Content-Type header is missing or when multiple Content-Type values are provided. These tests guard against regressions in id handling and content-type validation.
Add two tests to test/routes/error-messenger.test.js: one verifies the error messenger returns early when headers have already been sent (preserving partial response and leaving status 200), and another verifies that when an upstream error provides a text/plain Content-Type and a text() method, the messenger forwards the plain-text body and upstream status (example uses 418). These cover edge cases for header-sent behavior and handling of plain-text upstream responses.
Adds test/routes/rerum.test.js which verifies that fetchRerum maps fetch timeouts (AbortError) to a 504 upstream timeout error. The test mocks global.fetch to reject with an AbortError, sets RERUM_FETCH_TIMEOUT_MS to a low value, and asserts the returned error has status 504 and an appropriate message. Test setup/teardown restore the original global.fetch and environment variable.
Add a new test file for checkAccessToken (test/routes/tokens.test.js) to cover missing ACCESS_TOKEN, malformed JWTs, and refresh error propagation. Update error-messenger.test.js to assert statusCode/statusMessage fallback handling. Extend rerum.test.js to save/restore global setTimeout/clearTimeout and add tests that map non-timeout fetch failures to a 502 upstream error, ensure provided AbortSignal is forwarded, and verify fallback to the default timeout when the configured timeout is invalid.
Add unit tests for /query to cover upstream and network failure handling: (1) preserves upstream error text when fetch returns non-ok (503), (2) falls back to a generic RERUM error message when upstream .text() throws, and (3) maps rejected fetch (e.g., socket hang up) to a 502 response. Tests mock global.fetch and assert statusCode 502 and expected response text.
Add several unit tests to improve error handling coverage and token middleware behavior. create.test.js and overwrite.test.js gain tests that preserve upstream text on non-409 failures, fall back to a generic RERUM message when upstream .text() throws, and map missing-id successful payloads to 502. error-messenger.test.js replaces res.write with res.end in an existing test and introduces createMockRes plus focused messenger unit tests for headersSent, payload JSON, JSON content-type, and plain-text upstream errors. tokens.test.js adds tests ensuring valid or non-numeric-exp access tokens skip refresh. These changes increase robustness of error-path handling and token refresh logic in tests.
@cubap cubap requested a review from thehabes as a code owner April 16, 2026 21:06
@cubap cubap merged commit 8c5c36d into main Apr 16, 2026
5 checks passed
@cubap cubap deleted the 114-testing-testing branch April 16, 2026 21:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants